package com.Jiang.controller;

import com.Jiang.pojo.Book;
import com.Jiang.pojo.User;
import com.Jiang.service.BookService;
import com.Jiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;

//    //1.登录
//    @RequestMapping("/login")
//    public String Login(HttpSession httpSession,User user){
//        System.out.println(user);
//        System.out.println("登录");
//        String userId=user.getUserId ();
//        String password=user.getPassword();
//        Map m=new HashMap();
//        m.put("userId",userId);
//        List<User> users=userService.Login(m);
//        if(users.size()!=0&&users.get(0).getPassword().equals(password)){
//            System.out.println(users.get(0));
//            System.out.println("登录成功!");
//            httpSession.setAttribute("Login",userId);
//            System.out.println(httpSession.getAttribute("Login"));
//            return "redirect:/book/UserAllBook";
//        }
//
//        return "redirect:/loginUI";    //转发到主页
//    }
//    1.登录(改)
        @RequestMapping("/login")
    public String Login(HttpSession httpSession,User user){
        System.out.println(user);
        System.out.println("登录");
        String userId=user.getUserId ();
        String password=user.getPassword();
        Map m=new HashMap();
        m.put("userId",userId);
        List<User> users=userService.Login(m);
        if(users.size()!=0&&users.get(0).getPassword().equals(password)){
            System.out.println(users.get(0));
            System.out.println(users.get(0).getUserName());
            System.out.println("登录成功!");
            httpSession.setAttribute("Login",userId);
            httpSession.setAttribute("userName",users.get(0).getUserName());
            System.out.println(httpSession.getAttribute("Login"));
//            return "redirect:/book/mainBook";
            return "first";
        }

        return "redirect:/";    //转发到主页
    }
    //2.注销
    @RequestMapping("/logout")
    public String Logout(HttpSession httpSession){
        httpSession.removeAttribute("Login");
        return "redirect:/";
    }


//    3.登录界面
    @RequestMapping("/loginUI")
    public String LoginUI(){
        return "redirect:/";
    }

//    4.修改密码
    @RequestMapping("/toUpdataPassword")
    public String toUpdataPassword()
    {
        return "toUpdataPassword";
    }
    @RequestMapping("/UpdataPassword")
    public String UpdataPassword(HttpSession httpSession,String password){
        String userId= (String) httpSession.getAttribute("Login");
        Map m=new HashMap();
        m.put("userId",userId);
        m.put("password",password);
        System.out.println(m);
        userService.UpdataPassword(m);
        return "redirect:/loginUI";
    }
//5.验证
    @ResponseBody
    @RequestMapping("/checkOldPassword")
    public String checkOldPassword(HttpSession session, String oldPassword, HttpServletResponse response){
        String userId= (String) session.getAttribute("Login");
        Map m=new HashMap();
        m.put("userId",userId);
        String password=userService.Login(m).get(0).getPassword();
        if(password.equals(oldPassword)){
            return "√";
        }
            return "×";

    }
    //6.用户注册
    @RequestMapping("/toRegUser")
    public String toRegUser(){
        return "RegUser";
    }
    @RequestMapping("/RegUser")
    public String RegUser(User user){
        System.out.println("User==>"+user);
        userService.AddUser(user);
        return "redirect:/loginUI";
    }
//    7.用户个人信息修改
    @RequestMapping("/toUpdateUserInfor")
    public String toUpdateUserInfor(HttpSession httpSession, Model model){
        String userId =(String) httpSession.getAttribute("Login");
        User user = userService.selectBookById(userId);
        model.addAttribute("Quser",user);
        return "UpdateUserInfor";
    }
    @RequestMapping("/UpdateUserInfor")
    public String UpdateUserInfor(User user){
        System.out.println(user);
        userService.UpdataUser(user);
        return "UpdateUserInfor";
    }


}
